#ifndef	__MID_AUTOCHIP_H
#define __MID_AUTOCHIP_H
#include "ac780x_gpio.h"

#define CAN0_TX						GPIOB, GPIO_PIN0
#define CAN0_RX						GPIOB, GPIO_PIN1
#define CAN0_STB					GPIOC, GPIO_PIN8

#define GPIO_OUT_CAN_EN             GPIOB,GPIO_PIN6
#define GPIO_OUT_CAN_STB            GPIOC,GPIO_PIN8
#define GPIO_IN_CAN_FAULT           GPIOC,GPIO_PIN5

#define TEST_GPIO                   GPIOA,GPIO_PIN10

//---------------------------------mid_flash
#include "status.h"
#include "ac780x_eflash.h" 
#include "ac780x_eflash_reg.h"
#include "cmsis_armcc.h"
//typedef void (* flash_callback_t)(void);
typedef struct
{
    uint32_t PFlashBase;          /*!< The base address of P-Flash memory */
    uint32_t PFlashSize;          /*!< The size in byte of P-Flash memory */
    uint32_t DFlashBase;          /*!< For FlexNVM device, this is the base address of D-Flash memory (FlexNVM memory);
                                   *    For non-FlexNVM device, this field is unused */
    uint32_t DFlashSize;          /*!< For FlexNVM device, this is the size in byte of area
                                   *    which is used as D-Flash from FlexNVM memory;
                                   *    For non-FlexNVM device, this field is unused */
    uint32_t EERAMBase;           /*!< The base address of FlexRAM (for FlexNVM device)
                                   *    or acceleration RAM memory (for non-FlexNVM device) */
    uint32_t EEESize;             /*!< For FlexNVM device, this is the size in byte of EEPROM area which was partitioned
                                   *    from FlexRAM; For non-FlexNVM device, this field is unused */
    flash_callback_t CallBack;    /*!< Call back function to service the time critical events. Any code reachable from this function
                                   *   must not be placed in a Flash block targeted for a program/erase operation */
} flash_ssd_config_t;

extern status_t FLASH_DRV_EraseSector(const flash_ssd_config_t * pSSDConfig,uint32_t dest,uint32_t size);
extern status_t FLASH_DRV_Read(const flash_ssd_config_t * pSSDConfig,uint32_t dest, uint32_t size, uint8_t * pData);
extern status_t FLASH_DRV_Program(const flash_ssd_config_t * pSSDConfig,uint32_t dest, uint32_t size,const uint8_t * pData);
//------------------------------------can
#include "can_Cfg.h"
#include "CanIf_Types.h"
#include "can.h"
#include "ac780x_can.h"
#include "ac780x_can_reg.h"
#define CAN_INSTANCE_COUNT                       (3u)
#define INST_CAN0COM (0U)

#define	CAN0_EN_OUTPUT		       (GPIO_SetFunc(GPIO_OUT_CAN_EN, GPIO_FUN0),\
                                    GPIO_SetDir(GPIO_OUT_CAN_EN, GPIO_OUT),\
                                    GPIO_SetPullup(GPIO_OUT_CAN_EN,ENABLE))
#define	CAN0_EN_EN                  GPIO_SetPinLevel(GPIO_OUT_CAN_EN, GPIO_LEVEL_HIGH)
#define	CAN0_EN_DIS			        GPIO_SetPinLevel(GPIO_OUT_CAN_EN, GPIO_LEVEL_LOW)

#define	CAN0_STB_OUTPUT			   (GPIO_SetFunc(GPIO_OUT_CAN_STB, GPIO_FUN0),\
                                    GPIO_SetDir(GPIO_OUT_CAN_STB, GPIO_OUT),\
                                    GPIO_SetPullup(GPIO_OUT_CAN_STB,ENABLE))
#define	CAN0_STB_EN			        GPIO_SetPinLevel(GPIO_OUT_CAN_STB, GPIO_LEVEL_HIGH)
#define	CAN0_STB_DIS			    GPIO_SetPinLevel(GPIO_OUT_CAN_STB, GPIO_LEVEL_LOW)

#define	CAN0_WAKE_OUTPUT		    __NOP()
#define	CAN0_WAKE_DIS			    __NOP()
#define	CAN0_WAKE_EN			    __NOP()

#define	CAN0_FAULT_INPUT		    (GPIO_SetFunc(GPIO_IN_CAN_FAULT, GPIO_FUN0),\
                                     GPIO_SetDir(GPIO_IN_CAN_FAULT, GPIO_IN),\
                                     GPIO_SetPullup(GPIO_IN_CAN_FAULT,ENABLE))
#define	CAN0_FAULT_IS_HIGH           GPIO_GetPinLevel(GPIO_IN_CAN_FAULT)
extern can_state_t	can0_State;

extern void mid_can_Init(uint8_t instance);
extern CAN_STATUS CANBusState(uint8_t instance);
extern void Can_CanBusOffParaInit0(void);
extern void Can_CanBusOffTimeCnt(void);
extern Enum_Result_t mid_canRxIndication( uint8_t instance );
extern Enum_Result_t mid_canTxService( uint8_t instance );
extern void mid_canTimeout1ms(uint8_t instance);
extern void mid_can_busOffProc10ms(uint8_t instance);
void CAN0_IRQnCallBack(void *device, uint32_t wpara, uint32_t lpara);
static void CAN_HandleMsg(uint8_t instance, const CAN_MsgInfoType *msgInfo);
//-------------------------------------------------uart----------------------
extern void UART0_Service(void);
extern void UART1_Service(void);
//-------------------------------------------------mid_adc----------------------

//-------------------------------------uds_key-------------------------------
extern unsigned char SecM_ComputeKeyLevel1(unsigned long seed,unsigned long KeyK,unsigned long *key);
extern unsigned char SecM_ComputeKeyLevel2(unsigned long seed,unsigned long KeyK,unsigned long *key);
extern unsigned char SecM_ComputeKeyLevel3(unsigned long seed,unsigned long KeyK,unsigned long *key);
extern unsigned char SecM_ComputeKeyLevel4(unsigned long seed,unsigned long KeyK,unsigned long *key);
extern void mid_adc_init();
//----------------------------------mid_timer
#include "ac780x_timer.h"
#define Timer_CLK         (APB_BUS_FREQ)
#define DelayS(n)   (Timer_CLK*n-1)
#define Delay1ms    (Timer_CLK/1000-1)
#define Delay10ms   (Timer_CLK/100-1)


void TIMER_Clock(void);
#endif